import axios from 'axios';
import fs from 'fs';

const fileId = '7ryM2G71XRVwkQnkklWrbI';
const nodeId = '0:2753'; // 新页面节点
const token = process.env.FIGMA_ACCESS_TOKEN;

console.log('🔍 获取节点2753设计数据...');

try {
  const response = await axios.get(`https://api.figma.com/v1/files/${fileId}/nodes`, {
    headers: { 'X-Figma-Token': token },
    params: { ids: nodeId }
  });
  
  const nodeData = response.data.nodes[nodeId];
  
  if (nodeData && nodeData.document) {
    const doc = nodeData.document;
    console.log(`\n📱 页面: ${doc.name}`);
    console.log(`尺寸: ${doc.absoluteBoundingBox.width} x ${doc.absoluteBoundingBox.height}`);
    
    console.log('\n📋 页面元素分析:');
    
    doc.children.forEach((child, index) => {
      const box = child.absoluteBoundingBox;
      console.log(`\n${index + 1}. ${child.name} (${child.type})`);
      console.log(`   位置: x=${box.x}, y=${box.y}`);
      console.log(`   尺寸: ${box.width} x ${box.height}`);
      
      const relativeY = box.y - doc.absoluteBoundingBox.y;
      const relativeX = box.x - doc.absoluteBoundingBox.x;
      console.log(`   相对位置: x=${relativeX}, y=${relativeY}`);
      
      if (child.type === 'TEXT' && child.characters) {
        console.log(`   文本内容: "${child.characters}"`);
        if (child.style) {
          console.log(`   字体大小: ${child.style.fontSize || '未知'}`);
          console.log(`   字体粗细: ${child.style.fontWeight || '未知'}`);
        }
      }
      
      if (child.fills && child.fills.length > 0) {
        const fill = child.fills[0];
        if (fill.type === 'SOLID') {
          const color = fill.color;
          const hex = `#${Math.round(color.r * 255).toString(16).padStart(2, '0')}${Math.round(color.g * 255).toString(16).padStart(2, '0')}${Math.round(color.b * 255).toString(16).padStart(2, '0')}`;
          console.log(`   颜色: ${hex}`);
        }
      }
    });
    
    fs.writeFileSync('node-2753-data.json', JSON.stringify(nodeData, null, 2));
    console.log('\n💾 数据已保存到 node-2753-data.json');
    
    // 获取页面截图
    const imageResponse = await axios.get(`https://api.figma.com/v1/images/${fileId}`, {
      headers: { 'X-Figma-Token': token },
      params: {
        ids: nodeId,
        format: 'png',
        scale: 2
      }
    });

    const imageUrl = imageResponse.data.images[nodeId];
    if (imageUrl) {
      const imgResponse = await axios.get(imageUrl, { responseType: 'arraybuffer' });
      fs.writeFileSync('../entry/src/main/resources/rawfile/page_2753.png', imgResponse.data);
      console.log('✅ 页面截图已下载: page_2753.png');
    }
    
  }
  
} catch (error) {
  console.log('❌ 获取失败:', error.response?.status, error.response?.statusText);
  console.log('错误详情:', error.response?.data);
}
